<?php
namespace app\admin\controller;
use think\Controller;
use think\Db;
use think\Exception;
use app\admin\model\BatteryOrder as BatteryOrderModel;
use app\admin\model\BatteryGoods;
use think\Request;

// 订单控制
class BatteryOrder extends Common
{

    
    
    // 订单列表展示 分页筛选
    public function getOrderList(Request $request){
      
        $search_type_of_time=$request->param('search_type_of_time','');
        $atime=$request->param('atime','');
        $ztime=$request->param('ztime','');
        
        $status= $request->param('status', '0');

        if(!$atime || !$ztime){
            $search_type_of_time='';
            $atime='';
            $ztime='';
        }
        if($search_type_of_time ==''){
            $atime = '';
            $ztime = '';
        }

        $keyword=$request->param('keyword','');
        $order_type=$request->param('order_type',1);

        
        $list =  BatteryOrderModel::orderList($keyword,$order_type, $search_type_of_time, $atime, $ztime ,$status);

        
        $this->assign([
            'list'=>$list->paginate(),
            'keyword'=>$keyword,
            'order_type'=>$order_type,
            'search_type_of_time'=> $search_type_of_time,
            'atime'=>$atime,
            'ztime'=>$ztime,
            'status'=>$status,
        ]);

        return $this->fetch('battery_order/orderList');
    }

    // 更改 订单状态
    public function postAjaxChangeStatus(Request $request)
    {
        $order_id = $request->post('id');
        $status = $request->post('status');
        if(!is_numeric($order_id))
        {
            die(json_encode(['status'=>false,'msg'=>'参数错误']));
        }
        
       
        Db::startTrans();
        try{

            switch ($status)
            {
                case BatteryOrderModel::STATUS_PAY:
                    BatteryOrderModel::updatePayStatusByOrderId($order_id);
                    break;
                case BatteryOrderModel::STATUS_DELIVERY:
                     BatteryOrderModel::updateDeliveryStatusByOrderId($order_id);
                    break;
                case BatteryOrderModel::STATUS_COMPLETE:
                    BatteryOrderModel::updateCompleteStatusByOrderId($order_id);
                    break;
//                case BatteryOrderModel::STATUS_CANCEL:
//                    BatteryGoods::incrGoodsNum($orig_status['goods_id'],$orig_status['num']);
//                    BatteryOrderModel::updateCancelStatus($info_id);
//                    break;
                default:
                    break;
            }
        }catch (Exception $e)
        {
            Db::rollback();
            die(json_encode(['status'=>false,'msg'=>$e->getMessage()]));
        }
        
        Db::commit();
        die(json_encode(['status'=>true,'msg'=>'']));
        
    }

    // 订单列表展示 分页筛选
    public function getApplyCancelOrderList(Request $request){

        $keyword=$request->param('keyword','');
        $order_type=$request->param('order_type',1);

        $status= $request->param('status',0);
        $query = BatteryOrderModel::getApplyCancelOrderQuery($keyword,$status);


        $this->assign([
            'list'=>$query->paginate(),
            'keyword'=>$keyword,
            'order_type'=>$order_type,
            'status'=>$status,
        ]);

        return $this->fetch('battery_order/cancelOrderList');
    }
    public function postAjaxHandleCancelOrder(Request $request)
    {
        $order_id = $request->post('id');
        $status = $request->post('status');
        if(!is_numeric($order_id))
        {
            die(json_encode(['status'=>false,'msg'=>'参数错误']));
        }


        Db::startTrans();
        try{
            $info= BatteryOrderModel::getOrderInfo($order_id);
            if(empty($info))
            {
                throw  new Exception('没有该订单信息');
            }
            if($info['apply_cancel'] == 0 )
            {
                throw  new Exception('该订单没有申请取消');
            }
            if($info['apply_cancel'] >1)
            {
                throw  new Exception('该订单已处理，不能修改');
            }

            switch ($status)
            {
                case 2:
                    BatteryOrderModel::updateApplyCancelStatus($order_id,$status);
                    BatteryOrderModel::handleApplyOrderInfoCancel($order_id);
                    break;
                case 3:
                    BatteryOrderModel::updateApplyCancelStatus($order_id,$status);
                    break;
                default:
                    break;
            }
        }catch (Exception $e)
        {
            Db::rollback();
            die(json_encode(['status'=>false,'msg'=>$e->getMessage()]));
        }

        Db::commit();
        die(json_encode(['status'=>true,'msg'=>''])); 
    }
}
